﻿/* Copyright (c) Microsoft Corporation
 * 
 * All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.  You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * 
 * THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
 * 
 * See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProximityTapper
{
    /// <summary>
    /// Class for ServiceActivationHeader
    /// </summary>
    [CLSCompliant(false)]
    public class ServiceActivationHeader : IServiceActivationHeader
    {
        private static UInt32 MAX_ID = 8;
        private static UInt32 MAX_GUID = 16;
        private static UInt32 SERVICE_ACTIVATION_HEADER_MIN_SIZE = MAX_GUID + MAX_ID + 2 * sizeof(UInt16);

        // Source ID byte array (8 bytes)
        public byte[] sourceId = new byte[MAX_ID];

        public byte[] SourceId
        {
            get { return sourceId; }
            set { sourceId = value; }
        }

        // Service Activation UUID
        public Guid ServiceActivationGuid { get; private set; }

        // Extended Info 1 (16 bits)
        public UInt16 ExtendedInfo1 { get; private set; }

        // Service Version (16 bits)
        public UInt16 ServiceVersion { get; private set; }

        /// <summary>
        /// Try parsing ServiceActivationHeader
        /// </summary>
        /// <param name="data">Message Data</param>
        /// <param name="offset">
        /// Starting / current position in byte array.
        /// This is updated as the bytes are read.
        /// </param>
        /// <returns>true is successful in parsing, false otherwise</returns>
        /// <summary>
        bool IServiceActivationHeader.TryParse(byte[] data, ref UInt32 offset)
        {
            bool isParsed = true;

            try
            {
                // Verify that remaining data length is atleast minimum size 
                // required by Service Activation Header
                if (SERVICE_ACTIVATION_HEADER_MIN_SIZE <= (data.Length - offset))
                {
                    // Copy Source ID
                    Array.Copy(data, offset, this.SourceId, 0, MAX_ID);
                    offset += MAX_ID;

                    // Convert byte array to GUID
                    byte[] guid = new byte[MAX_GUID];
                    Array.Copy(data, offset, guid, 0, MAX_GUID);
                    this.ServiceActivationGuid = new Guid(guid);
                    offset += MAX_GUID;

                    this.ExtendedInfo1 = BigEndianConverter.ToUInt16(data, offset);
                    offset += sizeof(UInt16);

                    this.ServiceVersion = BigEndianConverter.ToUInt16(data, offset);
                    offset += sizeof(UInt16);
                }
                else
                {
                    // Parsing Error
                    isParsed = false;
                }
            }
            catch (Exception)
            {
                // Parsing Error
                isParsed = false;
            }

            return isParsed;
        }

        public override string ToString()
        {
            String output = "\n\nService Activation Header";

            output += "\nSource Id : " + Convert.ToBase64String(this.SourceId);

            if (this.ServiceActivationGuid != null)
            {
                output += "\nService Activation GUID : " + this.ServiceActivationGuid.ToString();
            }

            output += "\nExtended Info : " + StringFormatter.ToHexString(this.ExtendedInfo1);
            output += "\nService Version : " + StringFormatter.ToHexString(this.ServiceVersion);

            return output;
        }
    }
}
